package com.zerosoft.villagemonitor.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zerosoft.villagemonitor.config.PlatformProperties;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import jakarta.annotation.Resource;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@Data
public class HikPlatformService {
    @Resource
    private PlatformProperties platformProperties;

    protected ArtemisConfig artemisConfig(){
        ArtemisConfig artemisConfig = new ArtemisConfig();
        artemisConfig.setAppKey(platformProperties.getKey());
        artemisConfig.setAppSecret(platformProperties.getSecret());
        artemisConfig.setHost(platformProperties.getHost());
        return artemisConfig;
    }

    @SneakyThrows
    public String getRootRegion()
    {
        final String getCamsApi = platformProperties.getBase() + "/api/resource/v1/regions/root";
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
//        paramMap.put("pageNo", "1");
//        paramMap.put("pageSize", "2");
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put(platformProperties.getProtocol(), getCamsApi);
            }
        };

        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig(), path, body, null, null, "application/json");
    }

    @SneakyThrows
    public String getSubRegion(String regionId,String pageNo,String pageSize)
    {
        final String getCamsApi = platformProperties.getBase() + "/api/resource/v2/regions/subRegions";
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        paramMap.put("pageNo", pageNo);
        paramMap.put("pageSize", pageSize);
        paramMap.put("parentIndexCode", regionId);
        paramMap.put("resourceType", "region");
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put(platformProperties.getProtocol(), getCamsApi);
            }
        };

        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig(), path, body, null, null, "application/json");
    }
    @SneakyThrows
    public String getCamera(String regionId,String pageNo,String pageSize)
    {
        final String getCamsApi = platformProperties.getBase() + "/api/resource/v2/camera/search";
        Map<String, Object> paramMap = new HashMap<String, Object>();// post请求Form表单参数
        paramMap.put("pageNo", pageNo);
        paramMap.put("pageSize", pageSize);
        paramMap.put("regionIndexCodes", List.of(regionId));//JSON.toJSON(List.of(regionId)).toString());
        System.out.println(JSON.toJSON(List.of(regionId)).toString());

        String body = JSON.toJSON(paramMap).toString();
        System.out.println(body);
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put(platformProperties.getProtocol(), getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig(), path, body, null, null, "application/json");
    }

    @SneakyThrows
    public String getPreviewUrls(String cameraId)
    {
        final String getCamsApi = platformProperties.getBase() + "/api/video/v2/cameras/previewURLs";
        Map<String, Object> paramMap = new HashMap<String, Object>();// post请求Form表单参数
        paramMap.put("cameraIndexCode", cameraId);
        paramMap.put("streamType", platformProperties.getStreamType());
        paramMap.put("protocol", platformProperties.getStreamProtocol());
        paramMap.put("transmode", platformProperties.getTransmode());
        paramMap.put("expand", "transcode=1&videotype=h264");
        paramMap.put("streamType", platformProperties.getStreamType());

        String body = JSON.toJSON(paramMap).toString();
        System.out.println(body);
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put(platformProperties.getProtocol(), getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig(), path, body, null, null, "application/json");
    }

    @SneakyThrows
    public String getRegionInfo(String regionId)
    {
        final String getCamsApi = platformProperties.getBase() + "/api/resource/v1/region/regionCatalog/regionInfo";
        Map<String, Object> paramMap = new HashMap<String, Object>();// post请求Form表单参数
        paramMap.put("indexCodes", List.of(regionId));

        String body = JSON.toJSON(paramMap).toString();
        System.out.println(body);
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put(platformProperties.getProtocol(), getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig(), path, body, null, null, "application/json");

    }
}
